package com.chinawayltd.ews.sample.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.taskDispatch.*;
import com.chinawayltd.ews.sample.taskDispatch.pojo.BaseTask;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-07-12 18:47
 **/
public class MykDispatchAlgorithmServiceImpl {

    public void runningDispatch() throws Exception {
        //获取传入参数
        JSONObject inputData = getInputData();

        //订单信息
        JSONArray orders = inputData.getJSONArray(TaskDispatchConstants.TASK_LIST);
        //车辆信息
        JSONObject vehicle = inputData.getJSONObject(TaskDispatchConstants.VEHICLE);
        //站点耗时信息
        JSONArray timecost = inputData.getJSONArray(TaskDispatchConstants.TIMECOST);
        //站点信息
        JSONObject location = inputData.getJSONObject(TaskDispatchConstants.LOCATION);
//        JSONArray carlimit = inputData.getJSONArray(TaskDispatchConstants.CAR_LIMIT);
        //根据站点不可用车型算出站点可用车型
        handleLocation(location, inputData.getJSONArray(TaskDispatchConstants.CAR_LIMIT));
        //道路限行信息
        JSONArray roadlimit = inputData.getJSONArray(TaskDispatchConstants.ROADLIMIT);

        //创建订单仓库
        OrderDepot orderDepot = OrderDepot.initialize(orders);
        //创建车辆选择器
        TaskCarSelector carSelector = TaskCarSelector.initialize(vehicle, location);
        //任务生成器
        TaskGenerator taskGenerator = new TaskGenerator.Builder()
                .setLocation(location).setTimecost(timecost).setRoadlimit(roadlimit).build();

        List<JSONObject> taskList = new ArrayList<>();


        JSONObject fristTaskOrder = null;
        while ((fristTaskOrder = orderDepot.extractEarliestOrder()) != null){
            //根据任务第一个订单去寻找合适的车辆
            JSONObject vehicle1 = carSelector.choiceTransportVehicle(fristTaskOrder);
            if(vehicle1 == null)break;

            BaseTask task = new BaseTask(fristTaskOrder, vehicle1);

            JSONObject nextOrder = orderDepot.extractOrder();
            while (nextOrder != null){
                if(TaskOrderScreen.screeningOrder(task, nextOrder)){
                    task.addOrder(nextOrder);
                    nextOrder = orderDepot.extractOrder();
                }else{
                    nextOrder = orderDepot.returnOrderAndExtractNextRackOrder(nextOrder);
                }
            }

            taskList.add(taskGenerator.generaterTask(task));
        }

        System.out.println(taskList.size());

    }

    private void handleLocation(JSONObject location, JSONArray carlimit){
        final Integer[] carTypeTpl = new Integer[]{30, 20, 16};
        for (String key : location.keySet()) {
            JSONObject locationInfo = location.getJSONObject(key);
            List<Object> carTypeList = new ArrayList<>(Arrays.asList(carTypeTpl.clone()));
            JSONArray carType = new JSONArray(carTypeList);
            locationInfo.put(TaskDispatchConstants.LOCATION_CARTYPE, carType);
        }

        for (int i = 0; i < carlimit.size(); i++) {
            JSONObject timecostObj = carlimit.getJSONObject(i);
            Integer locid = timecostObj.getInteger(TaskDispatchConstants.CAR_LIMIT_LOCID);
            Integer carlimitVal = timecostObj.getInteger(TaskDispatchConstants.CAR_LIMIT_CARLIMIT);

            JSONObject locationInfo = location.getJSONObject(locid + "");
            JSONArray carType = locationInfo.getJSONArray(TaskDispatchConstants.LOCATION_CARTYPE);
            if(carType.contains(carlimitVal)){
                carType.remove(carlimitVal);
            }
        }
    }

    private JSONObject getInputData() throws IOException {
        StringBuilder sb = new StringBuilder();
        String line;
        BufferedReader br = new BufferedReader(new FileReader("d:/jsonIn.txt"));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();

        JSONObject jsonObject = JSONObject.parseObject(sb.toString());
        return jsonObject;
    }

    public static void main(String[] args) throws Exception {
        MykDispatchAlgorithmServiceImpl service = new MykDispatchAlgorithmServiceImpl();
        service.runningDispatch();
    }

}
